package com.climb.spingcloud.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Configuration

@ConditionalOnClass(RestTemplate.class)
public class ClimbRestTemplateConfiguration {
	@Bean
	@LoadBalanced
	@ConditionalOnMissingBean
	public RestTemplate restTemplate() {
		RestTemplate restTemplate = new RestTemplate();
		//消息转换器列表
		List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
		//配置消息转换器StringHttpMessageConverter，并设置utf‐8支持中文字符集
		messageConverters.set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
		return restTemplate;
	}
}